let arr = [-1, 2, 1, -4];
function getSum(target, arr) {
  // 升序排列
  arr.sort((a, b) => a - b);
  let min = Infinity;
  let res;
  for (let i = 0; i < arr.length; i++) {
    let basic = arr[i];
    let l = i + 1;
    let r = arr.length - 1;
    while (l < r) {
      let sum = basic + arr[l] + arr[r];
      let diff = Math.abs(sum - target);
      if (diff < min) {
        min = diff;
        res = [sum, l, r, i];
      }
      if (diff > 0) {
        r--;
      } else if (diff < 0) {
        l++;
      } else {
        return sum;
      }
    }
  }
  return res;
}
let res = getSum(1, arr);
console.log(res);
